
def Decoy_binomial_key_selection_button():

    global xrn_main_window_global
    slayout_local = xrn_main_window_global.jlayout_str

    nbinomial_keys = 0
    obj = get_from_layout ( slayout_local, "ring_signature_size" )
    for item in obj["fields"][2]["items"] :
        display_n = int ( item["display"] )
        if ( nbinomial_keys < display_n ) :
            nbinomial_keys = display_n

    selection = get_from_layout ( slayout_local, "Decoy_binomial_key_selection" )["fields"][2]["selection"] 
    selection_int = int(selection)

    for i in range ( 0, nbinomial_keys ):

        bkey_obj = get_from_layout ( slayout_local, "Decoy_binomial_key_" + str(i) )

        if i == selection_int :
            bkey_obj["permission"] = "read_write"
        else :
            bkey_obj["permission"] = "read_only"

        slayout_local = set_to_layout( slayout_local, bkey_obj )

    render_layout(  slayout_local )
    return 0

def rename_Decoy_binomial_key_button ():

    global xrn_main_window_global
    slayout_local = xrn_main_window_global.jlayout_str

    display = get_from_layout ( slayout_local, "rename_Decoy_binomial_key" )["fields"][2]["display"]
    selection = get_from_layout ( slayout_local, "Decoy_binomial_key_selection" )["fields"][2]["selection"] 

    obj = get_from_layout ( slayout_local, "Decoy_binomial_key_names" )
    obj["fields"][2]["items"][int(selection)]["data"] = display
    slayout_local = set_to_layout( slayout_local, obj )

    render_layout(  slayout_local )
    return 0

def view_Decoy_binomial_key_button ():

    global xrn_main_window_global
    slayout_local = xrn_main_window_global.jlayout_str
    error = 0; lib_info = 0
    logging_str, logging_info_str, cache_dir = generate_log_string( )
    architecture = get_from_layout( slayout_local, "running_bit_architecture" )["fields"][2]["selection"]

    panel_name = get_from_layout( slayout_local, "view_Decoy_binomial_key" )["fields"][0]["display"]
    popup_obj, row_index = start_xmessage_popup( panel_name , "input checks" )

    if error == 0 :
        Local_dump_encoding_param = get_from_layout ( slayout_local, "Local_encoding_parameters" )["fields"][2]["display"]
        error, popup_obj = check_encoding_param_wrapper( Local_dump_encoding_param, cache_dir, "", architecture, popup_obj, row_index )

    if error == 0 :
        keynum = get_from_layout ( slayout_local, "Decoy_binomial_key_selection" )["fields"][2]["selection"]
        Decoy_binomial_key = get_from_layout ( slayout_local, "Decoy_binomial_key_" + keynum )["fields"][2]["display"]
        error, popup_obj = check_binomial_key_wrapper( Decoy_binomial_key, cache_dir, Local_dump_encoding_param, "", architecture, popup_obj, row_index )

    if error == 0 :

        popup_obj, row_index = add_row_xmessage_popup( popup_obj, "view binomial key" )
        popup_obj[0]["rows"][row_index]["fields"].append({"type": "archive", "selection": "command", "items":[]})

        cmd_str = " xrnlib-cli --decoding-conf -encoding-param " + Local_dump_encoding_param + " -no-password "
        cmd_str = cmd_str  + " --permutation-conf -arch " + architecture + " " + logging_info_str
        cmd_str = cmd_str  + " --print-header -xrn-file " + Decoy_binomial_key + " "

        input_local_check, output_local_check, input_remote_check, output_remote_check,cmd_output = xrncmd_run ( cmd_str )

        popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": "command", "data": cmd_str })
        popup_obj, error = append_logs( popup_obj, cmd_str )

    if error == 0 :
        popup_obj = append_execution_logs( popup_obj, input_local_check, output_local_check, input_remote_check, output_remote_check )


    if error == 0 :
        index = 0
        index_item = 0
        for item in popup_obj[0]["rows"][row_index]["fields"][2]["items"]:
            if item["display"] == "lib-info-log" :
                index_item = index
                data = item["data"]
            index = index + 1
        data_obj = json.loads( data )
        data_obj["header"] = {} 
        popup_obj[0]["rows"][row_index]["fields"][2]["items"][index_item]["data"] = json.dumps(data_obj, indent=4 )

    if error == 0 :
        popup_obj[0]["display"] = "PASS : " + popup_obj[0]["display"] 
        popup_obj = append_result_in_archive( popup_obj, "pass" )

    else :                                          
        popup_obj[0]["display"] = "FAIL : " + popup_obj[0]["display"] 
        popup_obj = append_result_in_archive( popup_obj, "fail" )

    popup_str = json.dumps(popup_obj)

    os.system("rm -rf " + cache_dir)
    return popup_str

